package br.com.drestranho.escola.dao;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import br.com.drestranho.escola.entity.Chamada;
import br.com.drestranho.escola.entity.Funcionario;
import br.com.drestranho.escola.entity.Matricula;

@Stateless
public class ChamadaDAO {
	@PersistenceContext(unitName="H6Escola")
	private EntityManager em;
	
	public void salvar(Chamada bean) {
		em.persist(bean);
	}
	
	public void atualizar(Chamada bean) {
		em.merge(bean);
	}
	
	public void remover( Chamada bean) {
		em.remove(em.merge(bean));
	}
	
	public Chamada getBean(Integer id) {
		Chamada bean = em.find(Chamada.class, id);
		return bean;
	}
	
	public List<Chamada> getBeans(){//Lista 
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Chamada> cq = cb.createQuery(Chamada.class);
		Root<Chamada> from = cq.from(Chamada.class);
		CriteriaQuery<Chamada> select = cq.select(from);
		List<Chamada> Chamadas = em.createQuery(select).getResultList();
		return Chamadas;
	}
	
	// Obtém um List de anos letivos distintos existentes no banco de dados
	public List<Integer> carregaAnos() {
		Query qry = em.createQuery("select distinct t.ano from Chamada t");
		ArrayList<Integer> anos = (ArrayList<Integer>)qry.getResultList();
//		if (funcs.size() > 0) return funcs.get(0);
//		else 
		return anos;
	}

	
}
